import java.util.ArrayDeque;
import java.util.Deque;
import java.util.LinkedList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LIUYANJIAO
 * Date: 2024-10-17
 * Time: 11:50
 */
public class BinaryTree {

    static class TreeNode {
        public char val;
        public TreeNode left;//左孩子的引用
        public TreeNode right;//右孩子的引用

        public TreeNode(char val) {
            this.val = val;
        }
    }


    /**
     * 创建一棵二叉树 返回这棵树的根节点
     *
     * @return
     */
    public TreeNode createTree() {
        TreeNode a=new TreeNode('a');
        TreeNode b=new TreeNode('b');
        TreeNode c=new TreeNode('c');
        TreeNode d=new TreeNode('d');
        TreeNode e=new TreeNode('e');
        TreeNode f=new TreeNode('f');
        TreeNode g=new TreeNode('g');

        a.left=b;
        a.right=c;
        b.left=d;
        b.right=e;
       c.left=f;
        d.left=g;

        return a;
    }

    // 前序遍历
    public void preOrder(TreeNode root) {
        if(root==null) return ;
        System.out.print(root.val);

        preOrder(root.left);
        preOrder(root.right);

    }

    // 中序遍历
    void inOrder(TreeNode root) {
        if(root==null) return ;
        inOrder(root.left);
        System.out.print(root.val);
        inOrder(root.right);

    }

    // 后序遍历
    void postOrder(TreeNode root) {
        if(root==null) return ;
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val);
    }

    public static int nodeSize;

    /**
     * 获取树中节点的个数：遍历思路
     */
    void size(TreeNode root) {
        if(root==null) return ;
        nodeSize++;
        preOrder(root.left);
        preOrder(root.right);

    }

    /**
     * 获取节点的个数：子问题的思路
     *
     * @param root
     * @return
     */
    int size2(TreeNode root) {
        if(root==null) return 0;
        return size2(root.left)+size2(root.right)+1;
    }


    /*
     获取叶子节点的个数：遍历思路
     */
    public static int leafSize = 0;

    void getLeafNodeCount1(TreeNode root) {
        if(root==null) return ;
        if(root.right==null&&root.left==null){
            leafSize++;
        }
        preOrder(root.left);
        preOrder(root.right);
    }

    /*
     获取叶子节点的个数：子问题
     */
    int getLeafNodeCount2(TreeNode root) {
        if(root==null) return 0;
        if(root.right==null&&root.left==null) return 1;
        return getLeafNodeCount2(root.left)+getLeafNodeCount2(root.right);
    }

    /*
    获取第K层节点的个数
     */
    int getKLevelNodeCount(TreeNode root, int k) {
        if(root==null) return 0;
        if(k==1) return 1;
        return getKLevelNodeCount(root.left,k-1)+getKLevelNodeCount(root.right,k-1);
    }

    /*
     获取二叉树的高度
     时间复杂度：O(N)
     */
    int getHeight(TreeNode root) {
        if(root==null) return 0;
        return Math.max(getHeight(root.left),getHeight(root.right))+1;

    }


    // 检测值为value的元素是否存在
    TreeNode find(TreeNode root, char val) {

        if(root==null) return null;
        if(root.val==val){
            return root;
        }
        find(root.left,val);
        find(root.right,val);
        return null;
    }
    int a=0;

    //层序遍历
    void levelOrder(TreeNode root) {
        Deque<TreeNode> deque=new ArrayDeque<>();
        deque.add(root);
        while(!deque.isEmpty()){
            TreeNode t=deque.pop();
            System.out.print(t.val);
            if(t.left!=null){
                deque.add(t.left);
            }
            if(t.right!=null){
                deque.add(t.right);
            }
        }
    }


    // 判断一棵树是不是完全二叉树
    boolean isCompleteTree(TreeNode root) {
        if(root==null) return false;
        Deque<TreeNode> d=new LinkedList<>();
        d.add(root);
       while(!d.isEmpty()){
           TreeNode t=d.pop();
           if(t==null){
               while(!d.isEmpty()){
                   if(d.pop()!=null){
                       return false;
                   }
               }
               return true;
           }
           d.add(t.left);
           d.add(t.right);

       }
       return true;

//       if(root==null) return false;
//
//        if(root.left!=null&&root.right!=null){
//            return isCompleteTree(root.left)&&isCompleteTree(root.right);
//        }
//        else if(root.right==null&&root.left==null) return true;
//        else if(root.left!=null&&root.right==null) return false;
//        else  return false;

    }

    public static void main(String[] args) {
        BinaryTree binaryTree=new BinaryTree();
        TreeNode root=binaryTree.createTree();
        System.out.print("前序遍历：");
        binaryTree.preOrder(root);
        System.out.println();
        System.out.print("中序遍历：");
        binaryTree.inOrder(root);
        System.out.println();
        System.out.print("后序遍历：");
        binaryTree.postOrder(root);
        System.out.println();
        System.out.print("层序遍历：");
        binaryTree.levelOrder(root);
        System.out.println();

        System.out.println("树的节点个数："+binaryTree.size2(root));
        System.out.println("高度："+binaryTree.getHeight(root));
        System.out.println("第3层节点数："+binaryTree.getKLevelNodeCount(root,3));
        System.out.println("叶子节点数："+binaryTree.getLeafNodeCount2(root));
        System.out.println("是否为完全二叉树："+binaryTree.isCompleteTree(root));
    }
}